package org.linlinjava.litemall.wx.web;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallBrandCategory;
import org.linlinjava.litemall.db.service.*;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mall/brandCategory")
@Validated
public class WxBrandCategoryController {
    @Autowired
    private LitemallBrandCategoryService brandCategoryService;
    @Autowired
    private LitemallBrandService brandService;
    @Autowired
    private LitemallHotwordService hotwordService;
    @Autowired
    private LitemallServiceService serviceService;
    @Autowired
    private LitemallCategoryService  categoryService;//分类

    /**
     * @Description  查询公司拥有检测种类的情况，或者查询可以检测某个种类的公司情况
     * @Author Riko
     * @Date 2019/9/21
     **/
    @PostMapping("getAllBrandAndCategory")
    public Object getAllBrandAndCategory(@RequestBody String body, HttpServletRequest request) {
        Integer brandId = JacksonUtil.parseInteger(body, "brandId");
        Integer categoryId = JacksonUtil.parseInteger(body, "categoryId");
        Integer page = JacksonUtil.parseInteger(body, "page");
        Integer pageSize = JacksonUtil.parseInteger(body, "pageSize");
        if (page == null || pageSize == null) {
            return ResponseUtil.badArgument();
        }
        else {
            List<LitemallBrandCategory> allBrandAndCategory = brandCategoryService.getAllBrandAndCategory(brandId, categoryId, page, pageSize);
            List<LitemallBrandCategory>  brandCategories=new ArrayList<>();
            /*在实体类中新加    private String categoryName;*/
            for (LitemallBrandCategory litemallBrandCategory : allBrandAndCategory) {
                Integer categoryId1 = litemallBrandCategory.getCategoryId();
                String name = categoryService.findById(categoryId1).getName();
                litemallBrandCategory.setCategoryName(name);
                brandCategories.add(litemallBrandCategory);
            }
            return ResponseUtil.ok(brandCategories);
        }
    }
    
    /**
     * @Description  新增公司可以进行检测的项目
     * @Author Riko
     * @Date 2019/9/21
     **/
    @PostMapping("addCategoryForBrand")
    public Object addCategoryForBrand(@RequestBody String body, @LoginUser Integer userId) {
        Integer brandId = serviceService.getBelong(userId);
        List<Integer> categoryList = JacksonUtil.parseIntegerList(body, "categoryList");
        if (brandId == null || categoryList == null || categoryList.size() < 1) {
            return ResponseUtil.badArgument();
        }
        else {
            try {
                for (Integer i : categoryList) {
                    List<LitemallBrandCategory> list = brandCategoryService.getAllBrandAndCategory(brandId, i, 1, 100);
                    if (list != null && list.size() > 0) {
                        throw new Exception("此处禁止重复添加数据");
                    }
                }
                brandCategoryService.addCategoryForBrand(brandId, categoryList);
            } catch (Exception e) {
                return ResponseUtil.fail(500, e.toString());
            }
            return ResponseUtil.ok();
        }
    }
    
    /**
     * @Description  删除公司可以进行检测的项目
     * @Author Riko
     * @Date 2019/9/21
     **/
    @PostMapping("deleteCategoryForBrand")
    public Object deleteCategoryForBrand(@RequestBody String body, HttpServletRequest request) {
        Integer brandId = JacksonUtil.parseInteger(body, "brandId");
        List<Integer> categoryList = JacksonUtil.parseIntegerList(body, "categoryList");
        if (brandId == null || categoryList == null || categoryList.size() < 1) {
            return ResponseUtil.badArgument();
        }
        else {
            try {
                brandCategoryService.deleteCategoryForBrand(brandId, categoryList);
            } catch (Exception e) {
                return ResponseUtil.fail(500, e.toString());
            }
            return ResponseUtil.ok();
        }
    }
    //显示合作伙伴  每页公司个数 (如果前端不传值)
    static final int PAGESIZE_OF_CONDITON= 5;
    /**
     * @Description  筛选获得公司列表
     * @Author Riko
     * @Date 2019/9/21
     **/
    @PostMapping("queryByCondition")
    public Object queryByCondition(@RequestBody String body, HttpServletRequest request) {
        System.out.println("筛选获得公司列表入参>>>>" + body);
        Integer region = JacksonUtil.parseInteger(body, "region");
        Integer pCategory = JacksonUtil.parseInteger(body, "pCategory");
        String condition = JacksonUtil.parseString(body, "condition");
        List<Integer> categoryList = JacksonUtil.parseIntegerList(body, "category");
        List<Integer> scale = JacksonUtil.parseIntegerList(body, "scale");
        List<Integer> qualification = JacksonUtil.parseIntegerList(body, "qualification");
        Integer page = JacksonUtil.parseInteger(body, "page");
        Integer pageSize = JacksonUtil.parseInteger(body, "pageSize");
        if(null == pageSize || "".equals(pageSize)){ pageSize = PAGESIZE_OF_CONDITON;}
        String sort = JacksonUtil.parseString(body, "sort");
        String order = JacksonUtil.parseString(body, "order");
        if (page == null || pageSize == null) {
            return ResponseUtil.badArgument();
        }
        Map<Object, Object> result = new HashMap<>();
        int totalNum = brandService.getTotalNumByNum(region, pCategory, condition, categoryList, scale, qualification);
        result.put("totalNum", totalNum);
        result.put("list", brandService.queryByCondition(region, pCategory, condition, categoryList, qualification, scale, sort, order, page, pageSize));
        if (totalNum > 0 && StringUtils.isNotEmpty(condition)) {
            hotwordService.storeNewWord(condition);
        }
        return ResponseUtil.ok(result);
    }

    /**
     * @Description  获取五条最常用的搜索关键词
     * @Author Riko
     * @Date 2019/10/14
     **/
    @GetMapping("getHotWord")
    public Object getHotWord() {
        return ResponseUtil.ok(hotwordService.selectKeyword());
    }

    /*============================================================*/

    @PostMapping("/desc2")
    public Object huo(@RequestBody String data){
        /* "brandId": 1046024,
            "categoryId": 1036006,*/
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer page = jsonObject.getInteger("page");
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer brandId = jsonObject.getInteger("brandId");
        if( null == brandId || "".equals(brandId) || brandId < 0 ){ return ResponseUtil.fail(402,"请确认本公司的brandId"); }
          Integer categoryId = jsonObject.getInteger("categoryId");
        if( null == categoryId || "".equals(categoryId) || categoryId < 0){ return ResponseUtil.fail(402,"请确认类别信息的categoryId"); }
        if(null == page || "".equals(page) || page < 0){ page = 1;}
        //如果前端不传参数  默认10 条
        if(null == pageSize || "".equals(pageSize) || pageSize < 0){ pageSize = 10;}
        List<LitemallBrandCategory> allBrandAndCategory = brandCategoryService.getAllBrandAndCategory(brandId, categoryId, page, pageSize);
        return ResponseUtil.ok(allBrandAndCategory);


    }










}
